package com.jcf.springboot.security.filter;

import com.jcf.springboot.common.R;
import com.jcf.springboot.security.factory.LoginStrategyFactory;
import com.jcf.springboot.security.model.SecurityUser;
import com.jcf.springboot.security.service.MyUserdetailService;
import com.jcf.springboot.security.stragegy.LoginStragegy;
import com.jcf.springboot.security.util.JwtUtil;
import com.jcf.springboot.service.ManagerService;
import com.jcf.springboot.service.MemberService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.util.ObjectUtils;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @author ${chaofan.ji}
 * @since 2023/4/22
 */
@Slf4j
public class JwtAuthenticationFilter extends BasicAuthenticationFilter {

  @Autowired
  private JwtUtil jwtUtil;

  @Autowired
  private ManagerService managerService;

  @Autowired
  private MemberService memberService;

  @Autowired
  private MyUserdetailService myUserdetailService;

  @Autowired
  private JwtAuthenticationEntryPoint authenticationEntryPoint;

  @Autowired
  private LoginStrategyFactory loginStrategyFactory;


  public JwtAuthenticationFilter(AuthenticationManager authenticationManager) {
    super(authenticationManager);
  }

  @Override
  protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    System.out.println("shengchengtoken:" + new Date());
    String token = request.getHeader("token");
    log.info("请求url" + request.getRequestURI() + "token=  " + token);

    if (!ObjectUtils.isEmpty(token)
     && !(request.getRequestURL().toString().contains("/login")
     || request.getRequestURI().contains("/refreshToken")))
    {
      R r = jwtUtil.validateJWT(token);
      if (!r.getSuccess())
      {
        log.info(r.toString() + token);
        response.setStatus(r.getCode());
      }
      else
      {
        Claims claimByToken = jwtUtil.getClaimByToken(token);
        String username = claimByToken.getSubject();

        String loginType = request.getHeader("loginType");//策略模式
        LoginStragegy instance = loginStrategyFactory.getInstance(loginType);

        SecurityUser securityUser = instance.realLogin(username);
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =
         new UsernamePasswordAuthenticationToken(securityUser, null, securityUser.getPerms());
        SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);

        }

    }
    filterChain.doFilter(request, response);
  }


}
